package com.astersoft.lib.db;

/*
 * Copyright 2012 AsterSoft personal developer - Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.util.HashMap;

import com.astersoft.lib.db.filter.ASDBFilter;

/**
 * Custom default value class for the ASResultSetMapper class Here you can
 * define a custom default value for fields that mapper get null
 * 
 * @author dario durzo
 * @version 0.1
 * @date 29-05-2012
 */
public abstract class ASMapperDefaultValues
implements ASDBFilter {
	// internal mapping
	private HashMap<Class<?>, Object> mapping;

	/**
	 * Default constructor Initialize the custom default value mapping
	 * 
	 */
	public ASMapperDefaultValues() {
		mapping = new HashMap<Class<?>, Object>();
	}

	/**
	 * Get the mapping variable
	 * 
	 * @return
	 */
	protected HashMap<Class<?>, Object> getMapping() {
		return mapping;
	}

	/**
	 * Get custom mapping for data type
	 * 
	 * @param clObj
	 * @return
	 */
	public Object getCustomMapping(Class<?> clObj) {
		return mapping.get(clObj);
	}

	/**
	 * Add a custom mapping for object
	 * 
	 * @param clObj
	 * @param defaultValue
	 */
	public void addCustomMapping(Class<?> clObj, Object defaultValue) {
		mapping.put(clObj, defaultValue);
	}

	/**
	 * Add a complete custom mapping
	 * 
	 * @param mapping
	 */
	public void setMapping(HashMap<Class<?>, Object> mapping) {
		for (Class<?> cl : mapping.keySet()) {
			this.mapping.put(cl, mapping.get(cl));
		}
	}

	/**
	 * Overload method: set mapping from ASMapperDefaultValues object
	 * 
	 * @param values
	 */
	public void setMapping(ASMapperDefaultValues values) {
		this.setMapping(values.getMapping());
	}

	@Override
	public abstract Object doFilter(Object o);
}
